import { GlobalStore } from "../store/global";
import { Logger } from "../utils/Logger";
import { BaseModelProvider } from "./base";
import { BytedanceModelProvider } from "./bytedance";
import { DeepseekModelProvider } from "./deepseek";
import * as vscode from 'vscode';
import { parseModelName } from "./helper";
import { ModelInfo, ModelMesssage, ChunkMessage } from "./types";

export class ModelProviderFactory {
  static systemPropmt = '';
	static initialize(context: vscode.ExtensionContext) {
    const spFilePath = vscode.Uri.joinPath(context.extensionUri, 'docs', 'SystemPrompt.md');
    vscode.workspace.fs.readFile(spFilePath).then((data) => {
      this.systemPropmt = data.toString();
    });
	}
  
  static createModelProvider(modelInfo: ModelInfo): BaseModelProvider|null {
    const model = parseModelName(modelInfo);
    switch (model.manufacturer) {
      case 'deepseek':
        return new DeepseekModelProvider();
      case 'bytedance':
        return new BytedanceModelProvider();
      default:
        return null;
    }
  }

  static async createMessage(message: ModelMesssage[], context: vscode.ExtensionContext): Promise<AsyncGenerator<ChunkMessage> | undefined> {
    Logger.log('AI Code Agent: create message');
    const globalStore = new GlobalStore(context);
    const model = await globalStore.getModelInfo();
    Logger.log(JSON.stringify(model));
    const modelProvider = this.createModelProvider(model as ModelInfo);
    modelProvider?.init(model as ModelInfo);
    return modelProvider?.createMessage([
      {
        role: 'system',
        content: this.systemPropmt 
      } as ModelMesssage
    ].concat(message));
  }
}